package com.zyx.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyx.mapper.UserMapper;
import com.zyx.pojo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.xml.bind.DataBindingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;


    /**
     * 查询所有用户数量和每日新增数量
     *
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)//readOnly=true查询到的数据只读
    public Map<String, Integer> queryAll() {
        int l = userMapper.queryByAll();
        int i = userMapper.queryByTime();
        Map<String, Integer> map = new HashMap<>();
        map.put("data_size", l);
        map.put("data_new", i);
        return map;
    }

    /**
     * @Description: 用户登录功能
     * @Author: zyx
     * @Date: 11:39 2022/2/12
     * @Name: login
     * @Param: [user]
     * @Return: java.util.List<com.zyx.pojo.User>
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<User> login(User user) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        List<User> login = new ArrayList<>();
        if (user.getPhone() != null && !"".equals(user.getPhone().trim())) {
            criteria.andPhoneEqualTo(user.getPhone());
        }
        if (user.getPassWord() != null && !"".equals(user.getPassWord().trim())) {
            criteria.andPassWordEqualTo(user.getPassWord());
        }
        login = userMapper.selectByExample(example);
        return login;
    }

    /**
     * 模糊查询和查询所有数据
     *
     * @param pageNum
     * @param pageSize
     * @param user
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<User> queryAll(Integer pageNum, Integer pageSize, User user) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if (user != null) {
            //根据用户名称进行模糊查询
            if (user.getUserName() != null && !"".equals(user.getUserName().trim())) {
                criteria.andUserNameLike("%" + user.getUserName().trim() + "%");
            }
            if (user.getPhone() != null && !"".equals(user.getPhone().trim())) {
                criteria.andPhoneLike("%" + user.getPhone().trim() + "%");//根据手机号进行模糊查询
            }
            if (user.getCreateTime() != null) {
                criteria.andCreateTimeEqualTo(user.getCreateTime());//根据注册时间查询
            }
        }
        //分页
        PageHelper.startPage(pageNum, pageSize);//只对紧邻的查询语句有效，是用拦截器的方式实现
        List<User> list = userMapper.selectByExample(example);
        return new PageInfo<>(list);
    }

    /**
     * 添加用户信息
     *
     * @param user
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addUser(User user) {

        int i = 0;
        try {
            i = userMapper.insertSelective(user);
        } catch (Exception e1) {
            if (e1.getMessage().endsWith("for key 'user.one'")) {
                i = -1;
                System.out.println("手机号重复");
            } else {
                e1.printStackTrace();
            }
        }

        return i;
    }

    /**
     * 根据主键查询信息
     *
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public User selectByKeyUser(int id) {
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据主键删除----逻辑删除：实际开发中一般不允许把数据从磁盘内删除
     *
     * @param user
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteUser(User user) {
        user.setState(3);
        int i = userMapper.updateByPrimaryKey(user);
        return i;
    }

    /**
     * 根据主键动态更新数据
     *
     * @param user
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }
}
